$(function() {
    $('#com_bottom').load('../assets/public/html/PublicBottom.html');

    let companyHeader = '';
    //上传公司图标
    $('.Company_Avatar').on('change', function file_upload() {
        let form = document.querySelector('.com-re-touxiang')
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024; //图片最大
        companyHeader = file;
        // // 获取文件路径
        var filePath = $(this).val();
        // 截取上传文件的格式为".***"，从而进判断是否为图片格式
        var fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
        // 检查是否是图片格式
        if (!fileFormat.match(/.png|.jpg|.jpeg|.gif/)) {
            alert('上传错误,文件格式必须为：png/jpg/jpeg/gif');
            return;
        }
        //转成可以在本地预览的格式
        var src = window.URL.createObjectURL(this.files[0]);
        // 将图片路径赋值给img中的src
        let com_touxing = $('#com-re-touxiang').attr('src', src);
        if (com_touxing.length != 0) {
            $('#com-re-touxiang').show();
        }
    });

    //交互：职位类型：下拉框
    $('.company_con_reg_type_qianimg').click(function() {
        $('.company_con_reg_type_houimg').css('display', 'initial');
        $('.company_con_reg_type_ul').show();
        $('.company_con_reg_type_qianimg').hide();

        if ($('.company_con_reg_type_houimg').show()) {
            $('.company_con_reg_type_houimg').css('display', 'initial');
            $('.company_con_reg_type_ul').show();

            //将点击的li里的内容放入input中
            $('.company_con_reg_type_ul > li').click(function() {
                let ReleaseType = $(this).text();
                $('.com-type').val(ReleaseType);
                $('.company_con_reg_type_ul').hide();
                $('.company_con_reg_type_houimg').hide();
                $('.company_con_reg_type_qianimg').css('display', 'initial');
            })
        };
    });

    //判断是否是数值
    $('.com-pro-one-int').blur(function() {
        let protextone = $('.com-pro-one-int').val();
        if (protextone.length < 5) {
            $('.must_in4').show();
        } else {
            $('.must_in4').hide();
        }
    });
    $('.com-pro-two-int').blur(function() {
        let protexttwo = $('.com-pro-two-int').val();
        if (protexttwo.length < 5) {
            $('.must_in5').show();
        } else {
            $('.must_in5').hide();
        }
    });
    $('.com-num').blur(function() {
        let com_num = $('.com-num').val();
        if (isNaN(com_num)) {
            $('#Enter_value').show();
        } else {
            $('#Enter_value').hide();
        }
    });
    $('#con_pay_input_first').blur(function() {
        let com_reg_first = $('#con_pay_input_first').val();
        if (isNaN(com_reg_first)) {
            $('#Enter_value_n').show();
        } else {
            $('#Enter_value_n').hide();
        }
    });
    $('#con_pay_input_end').blur(function() {
        let com_reg_end = $('#con_pay_input_end').val();
        if (isNaN(com_reg_end)) {
            $('#Enter_value_n').show();
        } else {
            $('#Enter_value_n').hide();
        }
    });

    //成立时间
    function yearmo() {
        for (i = 1999; i <= 2022; i++) {
            const yaer_mo = `
                <li>${i}</li>
            `
            $('#con-year_type_ul').append(yaer_mo);
        }
    }

    yearmo();

    function menmo() {
        for (i = 1; i <= 12; i++) {
            const men_mo = `
                <li>${i}</li>
            `
            $('#con-men_type_ul').append(men_mo);
        }
    }

    menmo();

    function datamo() {
        for (i = 1; i <= 31; i++) {
            const data_mo = `
                <li>${i}</li>
            `
            $('#con-data_type_ul').append(data_mo);
        }
    }

    datamo();

    $('#con-year_type_qianimg').click(function() {
        $('#con-year_type_houimg').css('display', 'initial');
        $('#con-year_type_ul').show();
        $('#con-year_type_qianimg').hide();

        if ($('#con-year_type_houimg').show()) {
            $('#con-year_type_houimg').css('display', 'initial');
            $('#con-year_type_ul').show();

            //将点击的li里的内容放入input中
            $('#con-year_type_ul > li').click(function() {
                let year = $(this).text();
                $('#con-foundedtime-year').val(year);
                $('#con-year_type_ul').hide();
                $('#con-year_type_houimg').hide();
                $('#con-year_type_qianimg').css('display', 'initial');
            })
        };
    });
    $('#con-year_type_houimg').click(function() {
        $('#con-year_type_houimg').hide();
        $('#con-year_type_ul').hide();
        $('#con-year_type_qianimg').css('display', 'initial');
    });

    $('#con-men_type_qianimg').click(function() {
        $('#con-men_type_houimg').css('display', 'initial');
        $('#con-men_type_ul').show();
        $('#con-men_type_qianimg').hide();

        if ($('#con-men_type_houimg').show()) {
            $('#con-men_type_houimg').css('display', 'initial');
            $('#con-men_type_ul').show();

            //将点击的li里的内容放入input中
            $('#con-men_type_ul > li').click(function() {
                let men = $(this).text();
                $('#con-foundedtime-men').val(men);
                $('#con-men_type_ul').hide();
                $('#con-men_type_houimg').hide();
                $('#con-men_type_qianimg').css('display', 'initial');
                var yeartime = $('#con-foundedtime-year').val();
                if (men == 4 || men == 6 || men == 9 || men == 11) {
                    datatime(30);
                } else if (men == 1 || men == 3 ||
                    men == 5 || men == 7 || men == 8 ||
                    men == 10 || men == 12) {
                    datatime(31);
                } else if (men == 2 && yeartime % 400 == 0) {
                    datatime(29);
                } else if (men == 2) {
                    datatime(28);
                };
            });
        };

        function datatime(time) {
            $('#con-data_type_ul').html('');
            for (i = 1; i <= time; i++) {
                const data = `
                    <li onclick="datati">${i}</li>
                `
                $('#con-data_type_ul').append(data);
            };
        };
        datati = function(datime) {
            $('#con-foundedtime-data').val(datime);
            $('#con-data_type_ul').hide();
            $('#con-data_type_houimg').hide();
            $('#con-data_type_qianimg').css('display', 'initial');
        };

    });
    $('#con-men_type_houimg').click(function() {
        $('#con-men_type_houimg').hide();
        $('#con-men_type_ul').hide();
        $('#con-men_type_qianimg').css('display', 'initial');
    });

    $('#con-data_type_qianimg').click(function() {
        $('#con-data_type_houimg').css('display', 'initial');
        $('#con-data_type_ul').show();
        $('#con-data_type_qianimg').hide();

        if ($('#con-data_type_houimg').show()) {
            $('#con-data_type_houimg').css('display', 'initial');
            $('#con-data_type_ul').show();

            //将点击的li里的内容放入input中
            $('#con-data_type_ul > li').click(function() {
                let data = $(this).text();
                $('#con-foundedtime-data').val(data);
                $('#con-data_type_ul').hide();
                $('#con-data_type_houimg').hide();
                $('#con-data_type_qianimg').css('display', 'initial');
            })
        };
    });
    $('#con-data_type_houimg').click(function() {
        $('#con-data_type_houimg').hide();
        $('#con-data_type_ul').hide();
        $('#con-data_type_qianimg').css('display', 'initial');
    });
    let productPicture = [];

    //上传产品介绍
    $('.Product_picture_one').on('change', function file_upload() {
        let form = document.querySelector('.com-pro-one-in')
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024; //图片最大
        productPicture.push(file)
            // 获取文件路径
        var filePath = $(this).val();
        // 截取上传文件的格式为".***"，从而进判断是否为图片格式
        var fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
        // 检查是否是图片格式
        if (!fileFormat.match(/.png|.jpg|.jpeg|.gif/)) {
            alert('上传错误,文件格式必须为：png/jpg/jpeg/gif');
            return;
        }
        //转成可以在本地预览的格式
        var src = window.URL.createObjectURL(this.files[0]);
        // 将图片路径赋值给img中的src
        var com_pro_oneimg = $('#com-pro-one-in-img').attr('src', src);
        if (com_pro_oneimg.length != 0) {
            $('.com-pro-one-img').hide();
            $('#com-pro-one-in-img').show();
        }
    });
    $('.Product_picture_two').on('change', function file_upload() {
        let form = document.querySelector('.com-pro-two-in')
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024; //图片最大
        productPicture.push(file)
            // 获取文件路径
        var filePath = $(this).val();
        // 截取上传文件的格式为".***"，从而进判断是否为图片格式
        var fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
        // 检查是否是图片格式
        if (!fileFormat.match(/.png|.jpg|.jpeg|.gif/)) {
            alert('上传错误,文件格式必须为：png/jpg/jpeg/gif');
            return;
        }
        //转成可以在本地预览的格式
        var src = window.URL.createObjectURL(this.files[0]);
        // 将图片路径赋值给img中的src
        var com_pro_twoimg = $('#com-pro-two-in-img').attr('src', src);
        if (com_pro_twoimg.length != 0) {
            $('.com-pro-two-img').hide();
            $('#com-pro-two-in-img').show();
        }
    });

    const comImgArr = []
        //上传公司环境
    $('.com-en-in-file').on('change', function() {

        let form = document.querySelector('.com-en-in')
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024; //图片最大
        if (file.size > fileSize) {
            alert('上传失败，请上传不大于2M的图片！');
            return
        }
        comImgArr.push(file)


        // 获取文件路径
        var filePath = $(this).val();
        // 截取上传文件的格式为".***"，从而进判断是否为图片格式
        var fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();

        // 检查是否是图片格式
        if (!fileFormat.match(/.png|.jpg|.jpeg|.gif/)) {
            alert('上传错误,文件格式必须为：png/jpg/jpeg/gif');
            return;
        }
        //转成可以在本地预览的格式
        var src = window.URL.createObjectURL(this.files[0]);
        // 将图片路径赋值给img中的src
        if ($('.content-img-list-item-one > img').attr('src') == 0) {
            $('.content-img-list-item-one > img').attr('src', src);
            $('.content-img-list-item-one > img').css({
                'width': '300px',
                'padding-left': '30px',
                'margin-bottom': '40px'
            });
            $('.com_body_environment').css('position', 'inherit');
            $('.com-en-biao').css({
                'background': '#F6F6F6',
                'margin-left': '60px',
                'position': 'inherit'
            });
        } else if ($('.content-img-list-item-two > img').attr('src') == 0) {
            $('.content-img-list-item-two > img').attr('src', src);
            $('.content-img-list-item-two > img').css({
                'width': '300px',
                'padding-left': '30px',
                'margin-bottom': '40px'
            });
        } else {
            $('.content-img-list-item-three > img').attr('src', src);
            $('.content-img-list-item-three > img').css({
                'width': '300px',
                'padding-left': '30px',
                'margin-bottom': '40px'
            });
            $('.com-en-biao').hide();
        };
    });

    //触摸li 弹出（垃圾桶）
    $('.content-img-list-item-one').mouseover(function() {
        $('.content-img-list-item-one > .delete-btn').css('display', 'inline-block');
    }).mouseleave(function() {
        $('.content-img-list-item-one > .delete-btn').hide();
    });
    $('.content-img-list-item-two').mouseover(function() {
        $('.content-img-list-item-two > .delete-btn').css('display', 'inline-block');
    }).mouseleave(function() {
        $('.content-img-list-item-two > .delete-btn').hide();
    });
    $('.content-img-list-item-three').mouseover(function() {
        $('.content-img-list-item-three > .delete-btn').css('display', 'inline-block');
    }).mouseleave(function() {
        $('.content-img-list-item-three > .delete-btn').hide();
    });

    //删除图片
    $('.content-img-list-item-one > .delete-btn').click(function() {
        $('.content-img-list-item-one > img').attr('src', '').css('padding-left', '0px');
        $('.content-img-list-item-one > .delete-btn').hide();
        $('.com_body_environment').css('position', 'relative');
        $('.com-en-biao').css({
            'background': '#fff',
            'margin-left': '0px',
            'position': 'absolute'
        });
    });
    $('.content-img-list-item-two > .delete-btn').click(function() {
        $('.content-img-list-item-two > img').attr('src', '').css('padding-left', '0px');
        $('.content-img-list-item-two > .delete-btn').hide();
    });
    $('.content-img-list-item-three > .delete-btn').click(function() {
        $('.content-img-list-item-three > img').attr('src', '').css('padding-left', '0px');
        $('.content-img-list-item-three > .delete-btn').hide();
        $('.com-en-biao').show();
        $('.com_body_environment').css('position', 'inherit');
        $('.com-en-biao').css({
            'background': '#F6F6F6',
            'margin-left': '60px',
            'position': 'inherit'
        });
    });

    function judge(arr) {
        for (let item in arr) {
            if (arr[item] === undefined || arr[item] == '') {
                arr[item] = null;
            }
        }
    }

    $('#CompanyRegister_sub').click(function() {
        let Comretouxiang = companyHeader; // 公司图标
        let ComName = $('.com-name').val(); //公司名
        let ComType = $('.com-type').val(); //公司类型
        let ComNum = parseInt($('.com-num').val()); //公司职位数量
        let ComAdd = $('.com-add').val(); //公司地址
        let ComSalary = $('.com-salary').val(); //公司薪资
        let Combriefint = $('.com-brief_int').val(); // 公司简介
        let Comproonename = $('.com-pro-one-name').val(); //产品名称
        let Comproonecategory = $('.com-pro-one-category').val(); //产品类别
        let Comprooneint = $('.com-pro-one-int').val(); //产品简介
        let Comprotwname = $('.com-pro-two-name').val(); //产品名称
        let Comprotwocategory = $('.com-pro-two-category').val(); //产品类别
        let Comprotwoint = $('.com-pro-two-int').val(); //产品简介
        let Comwef = $('.com-wef').val(); //公司福利
        let Conenterprisetype = $('.con-enterprisetype').val(); //企业类型
        let Conbusinessscope = $('.con-businessscope').val(); //经营范围
        var con_inp_first = $('#con_pay_input_first').val();
        var con_inp_end = $('#con_pay_input_end').val();
        var con_input = con_inp_first + '-' + con_inp_end + 'K'; //注册金额
        let ConYear = $('#con-foundedtime-year').val();
        let ConMen = $('#con-foundedtime-men').val();
        let ConData = $('#con-foundedtime-data').val();
        let Con_foundedtime = ConYear + '-' + ConMen + '-' + ConData;
        let Confoundedtime = new Date(Con_foundedtime).getTime() / 1000; //成立时间
        let Conlegalrepresentative = $('.con-legalrepresentative').val(); //法定代表

        //判断是否填写必填项
        let pro_int_img = $('#com-pro-one-in-img').attr('src');
        if (pro_int_img.length == 0 || Comproonename.length == 0 ||
            Comproonecategory.length == 0 || Comprooneint.length == 0) {
            console.log("请输入完整必填项!");
        }

        const productNameArr = [Comproonename, Comprotwname];
        const productCategory = [Comproonecategory, Comprotwocategory]
        const productBrief = [Comprooneint, Comprotwoint]

        judge(productNameArr)
        judge(productCategory)
        judge(productBrief)
        judge(productPicture);

        if (productNameArr[1], productCategory[1], productBrief[1], productPicture[1] != null) {
            allFromData.append('product[]', productNameArr[1])
            allFromData.append('productType[]', productCategory[1])
            allFromData.append('product_introduction[]', productBrief[1])
            allFromData.append('productImg[]', productPicture[1])
        }

        let allFromData = new FormData();

        allFromData.append('company_logo', Comretouxiang);
        for (const item of comImgArr) {
            allFromData.append('environImg[]', item)
        }
        allFromData.append('productImg[]', productPicture[0]);


        allFromData.append('company_introduction', Combriefint);
        allFromData.append('company_name', ComName);
        allFromData.append('company_address', ComAdd);
        allFromData.append('company_welfare', Comwef);
        allFromData.append('range_money', ComSalary);
        allFromData.append('com_official', ComNum);
        allFromData.append('company_type', ComType);
        allFromData.append('boss_name', Conlegalrepresentative);
        allFromData.append('add_money', con_input);
        allFromData.append('management', Conbusinessscope);
        allFromData.append('company_addtime', Confoundedtime);
        allFromData.append('productType[]', productCategory[0]);
        allFromData.append('product[]', productNameArr[0]);
        allFromData.append('product_introduction[]', productBrief[0]);


        const registerCompany = upload('/registerCompany', 'post', allFromData)
        const { code } = registerCompany;
        const { message } = registerCompany;
        if (code == 500 && message == "该用户已有公司") {
            $('#com_body').hide();
            $('#com_Registration_succeeded').show();
            const failed = `
                <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/RedFalse.png" alt = "">
                <span>已有公司！</span> 
                <a href="index.html"">返回首页</a>
            `
            $('.com_Registration_succeeded').append(failed);
        } else if (code == 500) {
            $('#com_body').hide();
            $('#com_Registration_succeeded').show();
            const failed = `
                <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/RedFalse.png" alt = "">
                <span>发布失败！</span> 
                <a href="CompanyRegister.html">返回注册公司</a>
            `
            $('.com_Registration_succeeded').append(failed);
        } else if (code == 666) {
            $('#com_body').hide();
            $('#com_Registration_succeeded').show();
            const successfully = `
                <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/GreenBingo.png" alt="">
                <span>注册成功！</span>
                <a href="index.html">返回首页</a>
            `
            $('.com_Registration_succeeded').append(successfully);
        };
    });


});